extends Node2D

@onready var bullet_manager = $BulletMananger
@onready var player:Player =$Player
@onready var energy_manager = $EnergyManager
@onready var text_energy = $Label

@export var bmg1 :AudioStream
@export var bmg2 :AudioStream
@export var bmg3 :AudioStream


func _ready():
	randomize()
	GlobalSignals.connect("bullet_fired",Callable(bullet_manager, "handle_bullet_spawned"))
	# 监听所有敌人的处决信号
	#var enemies = get_tree().get_nodes_in_group("enemy")
	#for enemy in enemies:
	#	if not enemy.is_connected("executed", Callable(self,"_on_enemy_executed")):
	GlobalSignals.connect("being_executed",Callable(self,"_on_enemy_executed"))
	GlobalSignals.connect("beat_star",Callable(player,"handle_beat_star"))
	GlobalSignals.connect("beat_stop",Callable(player,"handle_beat_stop"))
	GlobalSignals.connect("energy_add",Callable(energy_manager,"energy_add"))
	GlobalSignals.connect("energy_state_change",Callable(self,"handle_energy_change"))
	
func _process(delta):
	text_energy.text = String.num(energy_manager.cur_energy)
	
func _on_enemy_executed(enemy):
	if enemy!=null and player.current_state != player.PlayerState.EXECUTE_FLIGHT:
		player.executing(enemy)

func handle_energy_change(state):
	match state:
		energy_manager.State.STATE1:
			print("play bgm 1 ==================")
			AudioManager.play_bgm(bmg1)
		energy_manager.State.STATE2:
			print("play bgm 2 ==================")
			AudioManager.play_bgm(bmg2)
		energy_manager.State.STATE3:
			print("play bgm 3 ==================")
			AudioManager.play_bgm(bmg3)
